
#ifndef CHORDKEYINFO_H_

#define CHORDKEYINFO_H_

#include "common.h"
#include "object.h"

class KeyTuple : public Object{
	public:
        KeyTuple() : online_(true) {}
#ifdef CHORD_POWER_OF_N
		TChordKeyVector keys_;

		chord_key_t& operator [] (size_t i) 
		{ return keys_[i]; }
		const chord_key_t& operator [] (size_t i) const 
		{ return keys_[i]; }
		void resize( int n ) { keys_.resize(n); }
        bool operator == (chord_key_t key){assert(false);return false;}
#else
		chord_key_t key_;

		chord_key_t& operator [] (size_t i) 
		{ return key_; }
		const chord_key_t& operator [] (size_t i) const 
		{ return key_; }
		void resize( int n ) { }
        bool operator == (chord_key_t key){ return (key==key_); }
#endif
        void setOnline(bool flag) {online_ = flag; }
        bool isOnline() const { return online_; }
        bool online_;
};

class ChordKeyInfo : public Object{
	public:
		enum STATUS{
			KEY_READY,
			KEY_SEARCHING
		};
		ChordKeyInfo( KeyTuple* keys, size_t index );
		~ChordKeyInfo();

#ifdef CHORD_POWER_OF_N
		ChordKeyInfo() : keys_(NULL), index_(0), status_(KEY_READY) {}
		chord_key_t getKey() const { return (*keys_)[index_]; }
		void        setKey(chord_key_t k) { (*keys_)[index_] = k; }
		size_t  	getIndex() const { return index_; }
		void 		setIndex( size_t i ) { index_ = i; }
#else
		ChordKeyInfo() : keys_(NULL), status_(KEY_READY){}
		chord_key_t getKey() const { return (*keys_).key_; }
		void        setKey(chord_key_t k) { (*keys_).key_ = k; }
#endif

		void		setReady() { status_=KEY_READY; }
		void 		setSearching() { status_=KEY_SEARCHING; }
		STATUS		getStatus() const { return status_; }
		bool 		isReady() { return (KEY_READY==status_); }

		ChordKeyInfo* copy();

	protected:
		KeyTuple*   keys_;
		STATUS 		status_;
#ifdef CHORD_POWER_OF_N
		size_t 		index_;
#endif
};

class __KEYINFO_KEY_PRED
{
public:
	__KEYINFO_KEY_PRED( chord_key_t key )
		: key_(key) {}
	bool operator () ( const ChordKeyInfo* info )
	{
		return ( info->getKey()==key_ ); 
	}
private:
	uint32_t key_;
};

#endif /* CHORDKEYINFO_H_ */

